class Solution:
    def romanToInt(self, s: str) -> int:
        if s is None:  # 不能为None
            return 0
        roman_dict = {
            "I": 1,
            "V": 5,
            "X": 10,
            "L": 50,
            "C": 100,
            "D": 500,
            "M": 1000
        }
        # 将字符串拆解为列表
        str_list = [i for i in s]
        # 进行相加
        total = 0
        i = 0
        list_len = len(str_list)
        while i < list_len:  # 不确定要遍历多少次
            temp = i + 1  # 临时索引
            flag1 = str_list[i] == 'I' and (temp) < list_len and (
                    str_list[temp] == 'V' or str_list[temp] == 'X')  # I的特殊情况
            flag2 = str_list[i] == 'X' and (temp) < list_len and (
                    str_list[temp] == 'L' or str_list[temp] == 'C')  # X的特殊情况
            flag3 = str_list[i] == 'C' and (temp) < list_len and (
                    str_list[temp] == 'D' or str_list[temp] == 'M')  # C的特殊情况
            if flag1 or flag2 or flag3:  # 对三种情况做特殊处理
                total += (roman_dict.get(str_list[temp]) - roman_dict.get(str_list[i]))
                i += 1  # 多加一次
            else:
                total += roman_dict.get(str_list[i])
            i += 1
        return total


luoma1 = "III"  # 3
luoma2 = "IV"  # 4
luoma3 = "IX"  # 9
luoma4 = "LVIII"  # 58
luoma5 = "MCMXCIV"  # 1994

# 测试
solution = Solution()
print(solution.romanToInt(luoma1))
print(solution.romanToInt(luoma2))
print(solution.romanToInt(luoma3))
print(solution.romanToInt(luoma4))
print(solution.romanToInt(luoma5))
